<%@page import="inventory.InventoryItem"%>
<%@page import="model.UserPage"%>
<%UserPage u = (UserPage) request.getAttribute("model");%>  
<html>
    <head>
        <link rel="stylesheet" type="text/css" href="css/friends.css"/>
        <script>
            $(document).ready(function() {
                $(".bookDetails").hide();
                $(".friend").children().hide();
                $(".friendName").fadeIn(300);
                
                $(".friendName").click(function () {
                    if($(this).parent().children(".book").is(":visible"))
                        $(this).parent().children(".book").fadeOut(300);
                    else
                        $(this).parent().children(".book").fadeIn(300);
                    $(this).parent().children(".bookDetails").hide();
                });
            
                $(".book").click(function(e) {
                    e.stopPropagation();
                });
                
                $(".bookTitle").click(function(e) {
                    var id = $(this).attr("id").substring(9);
                    if($("#bookDetails" + id).is(":visible"))
                        $("#bookDetails" + id).fadeOut(300);
                    else
                        $("#bookDetails" + id).fadeIn(300); 
                    e.stopPropagation(); 
                });
            });
        </script>
    </head>
    <body >
        <div style="padding:20px;">
            <%
                int IDIncrementor = 0;

                for (int i = 0; i < u.user.getFriendListSize(); ++i) {
                    out.print("<div class='friend' id='friend" + i + "'>");
                    out.print("<div class='friendName' id='friendName" + i + "'>");
                    out.print(u.user.getFriend(i).username);
                    out.print("</div>");

                    for (int j = 0; j < u.user.getFriend(i).getInventoryArraySize(); ++j) {
                        out.print("<div class='book' id='book" + IDIncrementor + "'>");

                        out.print("<div class='bookTitle' id='bookTitle" + IDIncrementor + "'>");
                        out.print(u.user.getFriend(i).getItem(j).itemName);
                        out.print("</div>");

                        out.print("<div id='bookDetails" + IDIncrementor + "' class='bookDetails'>");
                        out.print("<div class='bookAuthor'>");
                        out.print("By " + u.user.getFriend(i).getItem(j).itemAuthor);
                        out.print("</div>");

                        out.print("<div class='bookSummary'>");
                        out.print(u.user.getFriend(i).getItem(j).itemSummary);
                        out.print("</div>");

                        out.print("<div class='bookISBN'>");
                        out.print("ISBN: " + u.user.getFriend(i).getItem(j).itemISBN);
                        out.print("</div>");
                        out.print("</div>");

                        out.print("<div id='bookStatus" + IDIncrementor + "' class='bookStatus'>");
                        if (u.user.getFriend(i).getItem(j).status == 101) {
                            out.print("<div id='bookBorrow" + u.user.getFriend(i).username + IDIncrementor + "' class='bookBorrow'>");
                            out.print("Request to borrow this book");
                            out.print("</div>");
                        } else if (u.user.getFriend(i).getItem(j).userID == u.user.id) {
                            out.print("<div id='bookReturn" + u.user.getFriend(i).username + IDIncrementor + "' class='bookReturn'>");
                            out.print(u.user.getFriend(i).username + " lent you this book");
                            out.print("</div>");
                        } else if (u.user.getFriend(i).getItem(j).ownerID == u.user.id) {
                            out.print("<div id='bookRequest" + u.user.getFriend(i).username + IDIncrementor + "' class='bookRequest'>");
                            out.print(u.user.getFriend(i).username + " is borrowing your book");
                            out.print("</div>");
                        } else {
                            out.print("<div id='bookObserve" + u.user.getFriend(i).username + IDIncrementor + "' class='bookObserve'>");
                            out.print(u.user.getFriend(i).username + " has lent or is borrowing this from someone else");
                            out.print("</div>");
                        }
                        out.print("</div>");
                        out.print("</div>");
                        IDIncrementor++;
                    }
                    out.print("</div>");
                }                                                                          
            %>
        </div>
    </body>
</html>
